package com.nba.common.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.nba.common.cache.GlobalCache;
import com.nba.common.dao.IJdbcDao;
import com.nba.common.exception.CustomException;
import com.nba.common.form.RequestDataForm;
import com.nba.common.framework.ErrorCode;
import com.nba.common.framework.UserSessionData;
import com.nba.common.utils.Environment;
import com.nba.common.web.utils.RequestUtils;
import com.nba.system.cache.SysUrlCache;

/**
 * 封装通用方法
 * @author Jesse
 *
 */
public class BaseController {
	protected static Logger logger = Logger.getLogger(BaseController.class);

	@Autowired
	public IJdbcDao jdbcDao;

	public RequestDataForm getRequestDataForm(String id,
			HttpServletRequest request, HttpServletResponse response) throws CustomException,Exception {
		logger.debug("***************************");
		logger.debug("enter topic__"+id);
		logger.debug("***************************");
		// 权限
		// 查找对应的URL处理参数
		//Map<String, Object> urlSqlMap = jdbcDao.queryForMap(
		//		"select url_id, full_path, priv_id, title, page, url_type, cache_name, filter_priv, remark from sys_url t where t.url_id=?",
		//		new Object[] { id });
		Map<String, Object> urlCacheMap = GlobalCache.getCache(SysUrlCache.class, Map.class);
		if (urlCacheMap == null || urlCacheMap.size() == 0) {
		    throw new CustomException(-1,
                    "error! sys_url table is null! Please insert a record into the table sys_url.");
		}
		if (!urlCacheMap.containsKey(id)) {
			throw new CustomException(-1,
					"error! no url defined!Please insert a record into the table sys_url.");
		}
		
		RequestDataForm requestDataForm = RequestUtils
				.getRequestDataForm(request);
		requestDataForm.setUriId(id);
		requestDataForm.setUrlSqlMap((Map<String, Object>)urlCacheMap.get(id));
		requestDataForm.setUserSession((UserSessionData) request.getSession()
				.getAttribute(Environment.SESSION_USER_LOGIN_INFO)); 
		requestDataForm.setRequest(request);
		requestDataForm.setResponse(response);
		return requestDataForm;
	}
	/**
	 * 添加操作日志(在sys_op_log中添加操作记录)
	 */
	public void addOperateLog(HttpServletRequest request, String id, String title) throws CustomException{
		
		String ip = request.getRemoteAddr();//获取远程请求ip

		UserSessionData userSession = (UserSessionData)request.getSession().getAttribute(Environment.SESSION_USER_LOGIN_INFO);
		String user_id = "";
		String user_code = "";
		if(userSession==null){
			logger.error("userSession is null--===========================");
			throw new CustomException(ErrorCode.NO_LOGIN);
		} else {
			user_id = userSession.getUserId();
			user_code = userSession.getUserCode();
		}
		try {
			String insertSql = "insert into sys_op_log (login_ip, user_id, user_code, op_time, op_module, url, op_source) " +
					"values(?, ?, ?, now(), '0', ?, '0')";
			jdbcDao.execute(insertSql, new Object[]{ip, user_id, user_code, id});
		} catch (Exception e) {
			throw new CustomException(ErrorCode.DB_SAVE_ERROR);
		} 
	}
}
